// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// e [author] rhyme_lph
// W [email]  rhymelph@gmail.com
// l [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/utils/language_utils.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/hr_payroll/employee/views/particulars_provider.dart';
import 'package:finance_app/src/provider/hr_payroll/employee/new_employee_provider.dart';

class ParticularsView extends StatefulWidget {
  @override
  _ParticularsViewState createState() => _ParticularsViewState();
}

class _ParticularsViewState extends State<ParticularsView> with StatefulPresenter<ParticularsView, NewEmployeeProvider, ParticularsProvider>, KeepAliveMixin {
  @override
  Widget buildWidget(BuildContext context) => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            parentListBuilder<bool>(
              selector: (_, __) => __.id == null,
              builder: (BuildContext context, bool value, Widget child) => Visibility(
                child: child,
                visible: value,
              ),
              child: FormStatusWidget(
                enable: SizedBox(),
                disable: FromInputField(
                  controller: provider.employeeIdController,
                  focusNode: provider.employeeIdFocusNode,
                  tip: S.current.employee_id,
                ),
              ),
            ),
            FromInputField(
              controller: provider.nameController,
              focusNode: provider.nameFocusNode,
              tip: S.current.name_per_nric_passport,
            ),
            listBuilder<DateTime>(
              selector: (_, __) => __.birthDate,
              builder: (BuildContext context, DateTime value, Widget child) => ValueSelectForm(
                title: S.current.date_of_birth,
                value: DateTimeUtils.formatGlobalDateTime(value),
                onTap: provider.onChangeBirth,
              ),
            ),
            listBuilder<String>(
              selector: (_, __) => __.gender,
              builder: (BuildContext context, String value, Widget child) => ValueSelectForm(
                title: S.current.gender,
                value: LanguageUtils.translateStatus(value),
                onTap: provider.onChangeGender,
              ),
            ),
            listBuilder<String>(
              selector: (_, __) => __.race,
              builder: (BuildContext context, String value, Widget child) => ValueSelectForm(
                title: S.current.race,
                value: value,
                onTap: provider.onChangeRace,
              ),
            ),
            listBuilder<String>(
              selector: (_, __) => __.maritalStatus?.name,
              builder: (BuildContext context, String value, Widget child) => ValueSelectForm(
                title: S.current.marital_status,
                value: value,
                onTap: provider.onChangeMaritalStatus,
              ),
            ),
            listBuilder<String>(
              selector: (_, __) => __.passType?.name,
              builder: (BuildContext context, String value, Widget child) => ValueSelectForm(
                title: S.current.pass_type,
                value: value,
                onTap: provider.onChangePassType,
              ),
            ),
            listBuilder<bool>(
                selector: (_, __) => __.isShowPrStart,
                builder: (BuildContext context, bool value, Widget child) => Visibility(visible: value, child: child),
                child: listBuilder<String>(
                  selector: (_, __) => DateTimeUtils.formatGlobalDateTime(__.prStart),
                  builder: (BuildContext context, String value, Widget child) => ValueSelectForm(
                    title: S.current.pr_start_date,
                    value: value,
                    onTap: provider.onChangePrStartDate,
                  ),
                )),
            listBuilder<String>(
              selector: (_, __) => __.passType?.name,
              builder: (BuildContext context, String value, Widget child) =>
                  value != null && value.startsWith("Foreigner") ? _buildForeigner() : _buildLocalForm(),
            ),
            listBuilder<String>(
              selector: (_, __) => __.nationality?.name,
              builder: (BuildContext context, String value, Widget child) => ValueSelectForm(
                title: S.current.nationality,
                value: value,
                onTap: provider.onChangeNationality,
              ),
            ),
            AppSpaces.h50,
          ],
        ),
      );

  Widget _hasNeed() => Text(
        '*',
        style: TextStyle(
          color: aceRedColor,
        ),
      );

  Column _buildForeigner() {
    return Column(
      children: [
        FromInputField(
          controller: provider.nricController,
          focusNode: provider.nricFocusNode,
          tip: S.current.fin,
        ),
        listBuilder<String>(
          selector: (_, __) => DateTimeUtils.formatGlobalDateTime(__.passIssueDate),
          builder: (BuildContext context, String value, Widget child) => ValueSelectForm(
            title: S.current.pass_issue_date,
            value: value,
            onTap: provider.onChangePassIssueDate,
          ),
        ),
        listBuilder<String>(
          selector: (_, __) => DateTimeUtils.formatGlobalDateTime(__.passExpiryDate),
          builder: (BuildContext context, String value, Widget child) => ValueSelectForm(
            title: S.current.pass_expiry_date,
            value: value,
            onTap: provider.onChangePassExpiryDate,
          ),
        ),
      ],
    );
  }

  Column _buildLocalForm() {
    return Column(
      children: [
        FromInputField(
          controller: provider.nricController,
          focusNode: provider.nricFocusNode,
          tip: S.current.nric,
        ),
      ],
    );
  }
}
